<?php
/**
 * 支付类
 */

include_once FUN_PTH.'payment.inc.php';

class Payment extends MyBase{
	
	function __construct() {
		parent::__construct('payment.id', array('keyword' => array('name', 'desc')));
	}
	
	/**
	 * 修改支付
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		if($id <= 0)return false;
		$select = array('desc' => $post['desc'], 'kept' => " AND id!={$id}");
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){return lang('already').lang('exist');}
		if(strpos($post['fee'], '%') !== false){
			$fee = rtrim($post['fee'], '%');
			$feeunit = '%';
		}else{
			$fee = intval($post['fee']);
			$feeunit = '';
		}
		$data = array(
			'name'		=> $post['name'],
			'account'	=> $post['account'],
			'authkey'	=> $post['authkey'],
			'fee'		=> $fee,
			'feeunit'	=> $feeunit,
			'desc'		=> $post['desc'],
			'extra1'	=> $post['extra1'],
			'extra2'	=> $post['extra2'],
			'extra3'	=> $post['extra3'],
			'extra4'	=> $post['extra4'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_payment(); // 缓存支付
		}
		return $r;
	}
	
	public function update(){
		$payments = get_payments();
		foreach($payments['list'] as $mark => $payment){
			if(strpos($payment['fee'], '%') !== false){
				$fee = rtrim($payment['fee'], '%');
				$feeunit = '%';
			}else{
				$fee = intval($payment['fee']);
				$feeunit = '';
			}
			$data = array (
				'mark'		=> $mark,
				'name'		=> $payment['name'],
				'logo'		=> $payment['logo'],
				'url'		=> $payment['url'],
				'desc'		=> $payment['desc'],
				'online'	=> $payment['online'],
				'cod'		=> $payment['cod'],
				'fee'		=> $fee,
				'feeunit'	=> $feeunit,
				'feemod'	=> $feemod,
				'author'	=> $payment['author'],
				'version'	=> $payment['version'],
				'pubdate'	=> $payment['pubdate'],
				'dir'		=> $payment['dir'],
			);
			$rs = $this->get_row_by_mark($mark, '`id`');
			if(!empty($rs)){
				$data['updatetime'] = TIME;
				// $r = $this->db->update($this->tbname, array('id' => $rs['id']), $data);
			}else{
				$data['addtime'] = TIME;
				$r = $id = $this->db->insert($this->tbname, $data);
			}
		}
		return $this->cache_payment(); // 缓存支付
	}
		
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(!is_array($post['rank']))$post['rank'] = array($post['rank']);
		if(!is_array($post['state']))$post['state'] = array($post['state']);
		$iRtn = 0;
		foreach($post['lst'] as $id){
			$data = array(
				'rank'		 => $post['rank'][$id], 
				'updatetime' => TIME 
			);
			if(isset($post['state'][$id])){
				$data['state'] = 1;
			}else{
				$data['state'] = 0;
			}
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		return $this->cache_payment();
	}
	
	public function activate($post){
		return $this->rank($post);
	}
			
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	public function get_row_by_mark($mark, $fields = '*'){
		return $this->db->get_row($this->tbname, array('mark' => $mark), $fields);
	}
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}
	
	public function get_all($fields = '*', $select = array(), $order = array()){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_all($this->tbname, $fields, $select, $order);
	}*/
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
		
	public function cache_payment(){
		$list = $this->get_all('*', array('state' => 1), array('rank' => 'DESC', 'id' => 'ASC'));
		if(!empty($list['list'])){
			$payments = array();
			foreach($list['list'] as $payment){
				$payments[$payment['mark']] = $payment;
			}
			return cache_write('payments.php', $payments);
		}
		return true;
	}
	
}
?>